//package edu.ynu.se.xiecheng.achitectureclass.GreenHouse.Config;
//
//import org.apache.shiro.authc.*;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//
//public class CustomRealm extends AuthorizingRealm {
//
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//        // 这里实现授权逻辑
//        return null;
//    }
//
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
//        String username = upToken.getUsername();
//        String password = new String(upToken.getPassword());
//
//        // 这里实现身份验证逻辑
//        // 假设我们有一个用户名为 "admin"，密码为 "admin" 的用户
//        if ("admin".equals(username) && "admin".equals(password)) {
//            return new SimpleAuthenticationInfo(username, password, getName());
//        } else {
//            throw new AuthenticationException("用户名或密码错误");
//        }
//    }
//}
package edu.ynu.se.xiecheng.achitectureclass.GreenHouse.Config;


import edu.ynu.se.xiecheng.achitectureclass.GreenHouse.GreenhouseEntity.BigUser;
import edu.ynu.se.xiecheng.achitectureclass.GreenHouse.GreenhouseService.BigUserService;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class CustomRealm extends AuthorizingRealm {

    @Autowired
    private BigUserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        // 获取当前登录的用户名
        String username = (String) principals.getPrimaryPrincipal();

        // 从数据库中查询用户信息
        BigUser user = userService.findByUsername(username);

        // 创建授权信息对象
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();

        // 添加用户角色
        authorizationInfo.addRole(user.getRole());

        // 如果需要，可以添加用户的权限
        // authorizationInfo.addStringPermission("some:permission");

        return authorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        String username = upToken.getUsername();
        String password = new String(upToken.getPassword());

        // 从数据库中查询用户信息
        BigUser user = userService.findByUsername(username);

        // 验证用户是否存在
        if (user == null) {
            throw new UnknownAccountException("用户不存在");
        }

        // 验证密码是否正确
        if (!password.equals(user.getPassword())) {
            throw new IncorrectCredentialsException("密码错误");
        }

        // 返回认证信息
        return new SimpleAuthenticationInfo(username, password, getName());
    }
}